package iscript.uiFramework.components.silder
{
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.event.SilderEvent;
	import iscript.uiFramework.UIFramework;
	
	[Event(name="draging",type="iscript.uiFramework.event.SilderEvent")]
	
	internal class AbstractSilder extends UIComponent implements ISilder
	{
		private var _type:String;
		protected var silderDesinger:AbstractSilderDesinger;
		private var _minValue:int;
		private var _maxValue:int;
		private var _currentValue:int;
		private var _IsDrag:Boolean;
		
		protected var blockContainer:Sprite;
		protected var _inner:Boolean;
		private var _progress:Number;
		
		/**
		 * 拖动中是否实时发送Event
		 */
		public var liveDragging:Boolean;
		
		public function AbstractSilder()
		{
			_minValue = 0;
			_maxValue = 100;
			liveDragging = true;
			super();
		}
		
		override protected function initialize():void
		{
			super.initialize();
			silderDesinger.initialize();
			if (silderDesinger.topButton) {
				silderDesinger.block.x = silderDesinger.topButton.width;
				}
			silderDesinger.block.addEventListener(MouseEvent.MOUSE_DOWN, _silderInitDragLogic);
			UIFramework.UiStage.addEventListener(MouseEvent.MOUSE_MOVE, _silderDragLogic);
			UIFramework.UiStage.addEventListener(MouseEvent.MOUSE_UP, _silderStopDragLogic);
		}
		
		private function _silderInitDragLogic(e:MouseEvent):void
		{
			_IsDrag = true;
		}
		
		private function _silderStopDragLogic(e:MouseEvent):void
		{
			if(_IsDrag){
				_IsDrag = false;
				silderDesinger.block.stopDrag();
				dispatchSilderEvent();
			}
		}
		
		private function _silderDragLogic(e:MouseEvent):void
		{
			if (_IsDrag)
			{
				var rect:Rectangle = new Rectangle();
				if (!_inner)
				{
					rect.x =  _getUPButtonWidth() == 0?0:_getUPButtonWidth();
					rect.y = silderDesinger.block.y;
					rect.width = silderDesinger.bottomContainer.width;
				}
				else
				{
					rect.x =  _getUPButtonWidth()==0?0:_getUPButtonWidth()
					rect.y = silderDesinger.block.y;
					rect.width = silderDesinger.bottomContainer.width - silderDesinger.block.width;
				}
				silderDesinger.block.startDrag(false, rect);
				
				if (!_inner)
				{
					_currentValue = (silderDesinger.block.x-_getUPButtonWidth()) / silderDesinger.bottomContainer.width * valueBound;
				}
				if (_inner)
				{
					_currentValue = (silderDesinger.block.x-_getUPButtonWidth())/(silderDesinger.bottomContainer.width- silderDesinger.block.width) * valueBound;
				}
				
				silderDesinger.changeMask(_currentValue / valueBound * silderDesinger.bottomContainer.width);
				
				if (liveDragging)
				{
						dispatchSilderEvent()
				}
			}
		}
		
		protected function get viewRectangle():Rectangle
		{
			return UIFramework.resource.getElementRectangle("Silder", this.theme);
		}
		
		/**
		 *  class SilderType.Horizontal
		 */
		public function get type():String
		{
			return _type;
		}
		
		/**
		 *  class SilderType.Horizontal
		 */
		public function set type(value:String):void
		{
			_type = value;
			switch (value)
			{
				case SilderType.Horizontal: 
					silderDesinger.rotation = 0;
					silderDesinger.x = 0;
					break;
				case SilderType.Vertical: 
					silderDesinger.rotation = 90;
					silderDesinger.x = silderDesinger.bottomContainer.height;
					break;
			}
		}
		
		override public function set width(value:Number):void
		{
			silderDesinger.setWidth (value);
			super.width = value;
		}
		
		public function get minValue():int
		{
			return _minValue;
		}
		
		public function set minValue(value:int):void
		{
			_minValue = value;
		}
		
		public function get maxValue():int
		{
			return _maxValue;
		}
		
		public function set maxValue(value:int):void
		{
			_maxValue = value;
		}
		
		public function get currentValue():int
		{
			var value:int = _currentValue;
			//if (value != _minValue && value != _maxValue)
				value = _currentValue + _minValue;
			if (value > _maxValue)
				value = _maxValue;
			if (value < _minValue)
				value = _minValue;
			return value;
		}
		
		public function set currentValue(value:int):void
		{
			if (value > _maxValue)	value = _maxValue;
			if (value < _minValue)	value = _minValue;
			value = value - _minValue;
			
			_currentValue = value;
 
			if (_inner) {
				silderDesinger.block.x = (silderDesinger.bottomContainer.width-silderDesinger.block.width) * _currentValue / valueBound +_getUPButtonWidth();
				}else {					
				silderDesinger.block.x = silderDesinger.bottomContainer.width * _currentValue / valueBound +_getUPButtonWidth();
				}
			silderDesinger.changeMask(_currentValue / valueBound * silderDesinger.bottomContainer.width )
		}
		
		/**
		 * 进度百分比
		 */
		public function get progress():Number
		{
			return Number(_currentValue / valueBound);
		}
		
		/**
		 * 进度百分比
		 */
		public function set progress(value:Number):void
		{
			if (value < 0)
				value = 0;
			if (value > 1)
				value = 1;
			_progress = value;
			currentValue = value * valueBound;
		}
		
		/**
		 * value  0-1
		 * @param	value
		 */
		public function seek(value:Number):void
		{
			progress = value;
		}
		
		public function setBlock(target:DisplayObject):void
		{
			silderDesinger.setBlock(target);
		}
		
		protected function get valueBound():int
		{
			return _maxValue - _minValue;
		}
		
		/**
		 * Block在内部
		 */
		public function get inner():Boolean
		{
			return _inner;
		}
		
		/**
		 * Block在内部
		 */
		public function set inner(value:Boolean):void
		{
			_inner = value;
		}
		
		override public function render(container:DisplayObjectContainer = null):void
		{
			super.render(container);
		}
		
		override public function dispose():void
		{
			if (!Disposed)
			{
				silderDesinger.block.removeEventListener(MouseEvent.MOUSE_DOWN, _silderInitDragLogic);
				UIFramework.UiStage.removeEventListener(MouseEvent.MOUSE_MOVE, _silderDragLogic);
				UIFramework.UiStage.removeEventListener(MouseEvent.MOUSE_UP, _silderStopDragLogic);
				super.dispose()
			}
		}
		
		
		private function _getUPButtonWidth():Number {
			    if (silderDesinger.topButton == null) return 0;
				return silderDesinger.topButton.width;
			}
	
			
		internal function dispatchSilderEvent():void {
			var event:SilderEvent = new SilderEvent (SilderEvent.DRAGING)
				event.process = progress;
				this.dispatchEvent (event);
			}	
			
	}

}

